package com.kx.smartdevice.controller;

import com.kx.smartdevice.dto.HumTempEcharsDto;
import com.kx.smartdevice.dto.R;
import com.kx.smartdevice.pojo.Humiture;
import com.kx.smartdevice.service.HumitureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName HumitureController
 * @Author Sun
 * @Date 2021/6/2 19:17
 */
@RestController()
@RequestMapping("/humiture")
public class HumitureController {
    @Autowired
    private HumitureService humitureService;

    @RequestMapping("/humitures")
    public R getAllHumituresByTime() {
        List<Humiture> humituresList = humitureService.getHumituresByParamMap(new HashMap());
        List<Date> allTimes = new ArrayList<>();
        List<Double> allHum = new ArrayList<>();
        List<Double> allTemp = new ArrayList<>();
        humituresList.forEach(humiture -> { //将我们的数据独立分开存放,方便页面展示
            allTimes.add(humiture.getUploaddate());
            allHum.add(humiture.getHumidity());
            allTemp.add(humiture.getTemperature());
        });
        //为了方便我们返回数据,我们将数据封装到一个对象
        HumTempEcharsDto humTempEcharsDto = new HumTempEcharsDto();
        humTempEcharsDto.setHumidityList(allHum);
        humTempEcharsDto.setTemperatureList(allTemp);
        humTempEcharsDto.setTimes(allTimes);

        return R.setOK(humTempEcharsDto);
    }
}
